package org.akomantoso.core.data;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.akomantoso.core.data.group.BlockElements;
import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.Output;
import org.akomantoso.core.io.util.group.BlockElementsGroup;
import org.akomantoso.core.visitor.AKVisitor;

/**
 * The complex type blocksreq defines the content model and attributes shared by all containers. Here the id attribute
 * is required.
 * 
 * <pre>
 * 	<xsd:complexType name="blocksreq">
 * 		<xsd:sequence minOccurs="1" maxOccurs="unbounded">
 * 			<xsd:group ref="blockElements" />
 * 		</xsd:sequence>
 * 		<xsd:attributeGroup ref="corereq" />
 * 	</xsd:complexType>
 * </pre>
 * 
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 * 
 * @since http://www.akomantoso.org/1.0
 * 
 */
public abstract class Blocksreq extends CoreReqImpl {

	// Mandatory
	private final List<BlockElements> _elements = new ArrayList<BlockElements>();
	
	public List<BlockElements> getElements() {
		return _elements;
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#writeExternal(org.akomantoso.core.io.Output)
	 */
	public <T extends Output> void writeExternal(T out) throws IOException {
		super.writeExternal(out);
		for (BlockElements element : _elements) {
			element.writeExternal(out);
		}
	}
	
	/**
	 * @see org.akomantoso.core.io.Externalizable#readExternal(org.akomantoso.core.io.Input)
	 */
	public <T extends Input> void readExternal(T in) throws IOException {
		super.readExternal(in);
		
		final String localName = in.getLocalName();
		
		// goto next
		in.readNextStartElement();
		
		BlockElementsGroup.read(in, _elements,localName);
	}

	
	protected final void internalAccept(AKVisitor visitor) {
		if (_elements == null) {
			return;
		}
		for (BlockElements blockElement : _elements) {
			blockElement.accept(visitor);
		}
	}
}